package m3;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

public class Consumer {
    public static void main(String[] args) throws MQClientException {
        //新建消费者实例  mq吧消息推送给消费者
        DefaultMQPushConsumer c = new DefaultMQPushConsumer("consumer-group-3");

        //设置nameServer地址
        c.setNamesrvAddr("192.168.64.141:9876");

        //设置消息监听器
        c.setMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt mext:list){
                    String s = new String(mext.getBody());
                    System.out.println("收到:"+s);
                }
                //返回消息处理状态: 成功,稍后重新消费
//                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                System.out.println("处理消息失败");
                return ConsumeConcurrentlyStatus.RECONSUME_LATER; //稍后重试
            }
        });

        //设置接收的主题和标签
        c.subscribe("Topic3", "*");

        //启动
        c.start();

    }

}
